package com.woniuxy.myfinance.service.ServiceImpl;

import com.woniuxy.commons.entity.StaffLog;
import com.woniuxy.commons.utils.GetIp;

import com.woniuxy.myfinance.mapper.StaffLogMapper;
import com.woniuxy.myfinance.service.StaffLogService;
import org.aspectj.lang.JoinPoint;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2021-11-13 14:54
 */
@Service
public class StaffLogServiceImpl implements StaffLogService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;



    @Resource
    private StaffLogMapper staffLogMapper;


    @Override
    public void addStaffLog(JoinPoint joinPoint, String methodName, String module, String description) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = GetIp.getIpAddress(request);
        String username = (String) redisTemplate.opsForValue().get("username");
         StaffLog staffLog =new StaffLog();
         staffLog.setUsername(username);
         staffLog.setOperatortime(new Date());
         staffLog.setContent(description);
         staffLog.setHostip(ip);
         staffLog.setModule(module);
         staffLogMapper.addStaffLog(staffLog);

    }

    @Override
    public List<StaffLog> getAllStaffLog(Integer pageIndex, Integer pageSize) {
        List<StaffLog> allStaffLog = staffLogMapper.getAllStaffLog(pageIndex, pageSize);
        return allStaffLog;
    }

    @Override
    public void delStaffLog(int logid) {
        staffLogMapper.delStaffLog(logid);
    }

    @Override
    public int selectStaffLogCount() {
        return staffLogMapper.selectStaffLogCount();
    }
}
